package com.qf.main.pharmacy.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.main.pharmacy.common.qo.DeliveryQo;
import com.qf.main.pharmacy.common.qo.StorageQo;
import com.qf.main.pharmacy.common.vo.AuditVo;
import com.qf.main.pharmacy.common.vo.DeliveryVo;
import com.qf.main.pharmacy.common.vo.StorageVo;
import com.qf.main.pharmacy.entity.Delivery;
import com.qf.main.pharmacy.entity.Storage;
import com.qf.main.pharmacy.mapper.DeliveryMapper;
import com.qf.main.pharmacy.mapper.PharmacyInventoryMapper;
import com.qf.main.pharmacy.service.DeliveryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author chengqiang
 * @Create 2024-03-03-23:32
 * @Description:
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {

    @Resource
    private DeliveryMapper deliveryMapper;
    @Resource
    private PharmacyInventoryMapper pharmacyInventoryMapper;



    @Override
    public Integer saveDelivery(DeliveryQo deliveryQo) {
        Delivery delivery = CommonBeanutils.convertTo(deliveryQo, Delivery::new);
        delivery.setPrice(new BigDecimal(deliveryQo.getPrice()));
        delivery.setTotalPrice(new BigDecimal(deliveryQo.getTotalPrice()));
        Snowflake snowflake = new Snowflake();
        Long no = snowflake.nextId();
        delivery.setDeliveryNo(no);
        deliveryMapper.insert(delivery);

        pharmacyInventoryMapper.increaseClaimQuantity(delivery.getPharmacyId(),delivery.getQuantity());
        return 1;
    }

    @Override
    public List<AuditVo> orderByIdList(int pageNum, int pageSize, int id) {
        return null;
    }

    @Override
    public PageInfo<DeliveryVo> deliveryList(Integer pageNum, Integer pageSize, String deliveryNo) {


        QueryWrapper<Delivery> qw = new QueryWrapper<>();
        qw.eq(!StringUtils.isEmpty(deliveryNo), Delivery.COL_DELIVERY_NO,deliveryNo);
        PageInfo<DeliveryVo> PageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> CommonBeanutils.convertTo(deliveryMapper.selectList(qw), StorageVo::new));
        return PageInfo;
    }

    @Override
    public Integer modifyDelivery(StorageQo storage) {
        return null;
    }

    @Override
    public Integer removeDelivery(int id) {
        return null;
    }
}
